﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using DTO;

namespace nhakhoachauau
{
    public partial class WebForm8 : System.Web.UI.Page
    {
        public decimal TongPhi;
        protected void Page_Load(object sender, EventArgs e)
        {
            TongPhi = 0;
            if (!IsPostBack)
            {
                dp1.Value = DateTime.Now.Date.ToString("MM-dd-yyyy");
                div_ChonNgay.Visible = true;
                div_ChonThang.Visible = false;
                div_ChonNam.Visible = false;
                lblTitle.Text = "Ngày " + dp1.Value;
            }
        }

        protected void ddlChonNam_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                for (int i = DateTime.Now.Year; i >= 2000; i--)
                {
                    ddlChonNam.Items.Add(new ListItem(i.ToString(), i.ToString()));
                }
            }
        }

        protected void ddlThongKe_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (int.Parse(ddlThongKe.SelectedValue) == (int)enumThongKe.TheoNgay)
            {
                div_ChonNgay.Visible = true;
                div_ChonThang.Visible = false;
                div_ChonNam.Visible = false;
                dp1.Value = DateTime.Now.Date.ToString("MM-dd-yyyy");

                rptThongKeTheoNgay.Visible = true;
                rptThongKeTheoThang.Visible = false;
                rptThongKeTheoNam.Visible = false;

                //lblTitle.Text = "Ngày " + dp1.Value;
            }
            else if (int.Parse(ddlThongKe.SelectedValue) == (int)enumThongKe.TheoThang)
            {
                div_ChonNgay.Visible = false;
                div_ChonThang.Visible = true;
                div_ChonNam.Visible = false;
                txtChonThang.Value = DateTime.Now.Date.ToString("MM-yyyy");

                rptThongKeTheoNgay.Visible = false;
                rptThongKeTheoThang.Visible = true;
                rptThongKeTheoNam.Visible = false;

                //lblTitle.Text = "Tháng " + txtChonThang.Value;
            }
            else if (int.Parse(ddlThongKe.SelectedValue) == (int)enumThongKe.TheoNam)
            {
                div_ChonNgay.Visible = false;
                div_ChonThang.Visible = false;
                div_ChonNam.Visible = true;

                rptThongKeTheoNgay.Visible = false;
                rptThongKeTheoThang.Visible = false;
                rptThongKeTheoNam.Visible = true;

                //lblTitle.Text = "Năm " + ddlChonNam.Text;
            }
            btnThongKe_Click(sender, e);
        }

        protected void rptThongKeTheoNgay_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Q_BUSChiTietKhamBenh BUSphi = new Q_BUSChiTietKhamBenh();
                string[] arrValue = dp1.Value.Split('-');
                List<PhiTheoNgay> dsPhiNgay = BUSphi.dsPhiKhamBenhTheoNgay(new DateTime(int.Parse(arrValue[2]), int.Parse(arrValue[0]), int.Parse(arrValue[1])));
                if (dsPhiNgay == null)
                    dsPhiNgay = new List<PhiTheoNgay>();
                rptThongKeTheoNgay.DataSource = dsPhiNgay;
                rptThongKeTheoNgay.DataBind();
            }
        }
        //Hoàn thành hình thức
        protected void rptThongKeTheoNgay_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                PhiTheoNgay phi = (PhiTheoNgay)e.Item.DataItem;
                Label lblSTT = (Label)e.Item.FindControl("lblSTT");
                Label lblBenhNhan = (Label)e.Item.FindControl("lblBenhNhan");
                Q_BUSBenhNhan BUSbn = new Q_BUSBenhNhan();
                lblBenhNhan.Text = BUSbn.ThongTinBenhNhan(phi.ID_BenhNhan).ho_ten;
                TongPhi += phi.TongTien;
                lblSTT.Text = (e.Item.ItemIndex + 1).ToString();
            }
        }
        //Chưa hoàn thành
        protected void rptThongKeTheoThang_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ChiTheoThang chi = (ChiTheoThang)e.Item.DataItem;
                Label lblSTT = (Label)e.Item.FindControl("lblSTT");
                TongPhi += (decimal)chi.TongChiThang;
                lblSTT.Text = (e.Item.ItemIndex + 1).ToString();
            }
        }
        //Chưa hoàn thành
        protected void rptThongKeTheoNam_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ChiTheoNam chi = (ChiTheoNam)e.Item.DataItem;
                Label lblSTT = (Label)e.Item.FindControl("lblSTT");
                TongPhi += (decimal)chi.TongTien;
                lblSTT.Text = (e.Item.ItemIndex + 1).ToString();
            }
        }
        protected void btnThongKe_Click(object sender, EventArgs e)
        {
            if (int.Parse(ddlThongKe.SelectedValue) == (int)enumThongKe.TheoNgay)
            {
                Q_BUSChiTietKhamBenh BUSphi = new Q_BUSChiTietKhamBenh();
                string[] arrValue = dp1.Value.Split('-');
                List<PhiTheoNgay> dsPhiNgay = BUSphi.dsPhiKhamBenhTheoNgay(new DateTime(int.Parse(arrValue[2]), int.Parse(arrValue[0]), int.Parse(arrValue[1])));
                if (dsPhiNgay == null)
                    dsPhiNgay = new List<PhiTheoNgay>();
                rptThongKeTheoNgay.DataSource = dsPhiNgay;
                rptThongKeTheoNgay.DataBind();
                lblTitle.Text = "Ngày " + dp1.Value;

            }
            else if (int.Parse(ddlThongKe.SelectedValue) == (int)enumThongKe.TheoThang)
            {
                Q_BUSChiTietKhamBenh BUSctkb = new Q_BUSChiTietKhamBenh();
                string[] arrValue = txtChonThang.Value.Split('-');
                List<ChiTheoThang> dsPhiThang = BUSctkb.dsPhiKhamBenhTheoThang(int.Parse(arrValue[0]), int.Parse(arrValue[1]));
                if (dsPhiThang == null)
                    dsPhiThang = new List<ChiTheoThang>();
                rptThongKeTheoThang.DataSource = dsPhiThang;
                rptThongKeTheoThang.DataBind();
                lblTitle.Text = "Tháng " + txtChonThang.Value;
            }
            else
            {
                Q_BUSChiTietKhamBenh BUSctkb = new Q_BUSChiTietKhamBenh();
                int nam = int.Parse(ddlChonNam.Text);
                rptThongKeTheoNam.DataSource = BUSctkb.dsPhiKhamBenhTheoNam(nam);
                rptThongKeTheoNam.DataBind();
                lblTitle.Text = "Năm " + ddlChonNam.Text;
            }
        }
    }
}